

//
//  ForgetSecretView.m
//  XJMTechnician
//
//  Created by Mr.hu on 2017/10/17.
//  Copyright © 2017年 Mr.hu. All rights reserved.
//

#import "ForgetSecretView.h"

@interface ForgetSecretView ()<UITextFieldDelegate>
{
    NSInteger _runLoopTimes;
    NSTimer * unregisteredTimer;
}
@property (weak, nonatomic) IBOutlet UIButton *ForgetUpdateSubmitBtn;

@property (weak, nonatomic) IBOutlet UITextField *InputPhotoNumber;
@property (weak, nonatomic) IBOutlet UITextField *VerificationCode;
@property (weak, nonatomic) IBOutlet UITextField *InputNewSecret;
@property (weak, nonatomic) IBOutlet UITextField *InputSureSecret;

@property (weak, nonatomic) IBOutlet UIButton *VerificationReceiveBtn;

@property (weak, nonatomic) IBOutlet UIView *FirstForgetView;
@property (weak, nonatomic) IBOutlet UIView *SecondForgetView;
@property (weak, nonatomic) IBOutlet UIView *ThreeForgetView;
@property (weak, nonatomic) IBOutlet UIView *FourForgetView;

@property (nonatomic,copy) NSString * codeID;

@end

@implementation ForgetSecretView

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBtn:[UIImage imageNamed:@"fanhui"] Action:@selector(backToPrevious)];
    self.title = @"忘记密码";
    
    self.InputPhotoNumber.delegate = self;
    self.VerificationCode.delegate = self;
    self.InputNewSecret.delegate = self;
    self.InputSureSecret.delegate = self;
    
    self.VerificationReceiveBtn.layer.cornerRadius  = 15;
    self.VerificationReceiveBtn.layer.masksToBounds = YES;
    
    self.ForgetUpdateSubmitBtn.layer.cornerRadius  = 15;
    self.ForgetUpdateSubmitBtn.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jkr_tapGestureAction:)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void)jkr_tapGestureAction:(UITapGestureRecognizer *)tapGusture{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.InputSureSecret resignFirstResponder];
    [self.VerificationCode resignFirstResponder];
    [self.InputNewSecret resignFirstResponder];
    [self.InputPhotoNumber resignFirstResponder];
    return NO;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.InputSureSecret resignFirstResponder];
    [self.VerificationCode resignFirstResponder];
    [self.InputNewSecret resignFirstResponder];
    [self.InputPhotoNumber resignFirstResponder];
}


- (IBAction)ClearPhoneNumber:(id)sender {
    self.InputPhotoNumber = nil;
}

- (IBAction)ReceiveVerificationCodeMeth:(id)sender {
    
    if (self.InputPhotoNumber.text.length ==11) {
        
        _runLoopTimes = 0;
        self.VerificationReceiveBtn.userInteractionEnabled = NO;
        if(unregisteredTimer == nil) {
            unregisteredTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(runLoop) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:unregisteredTimer forMode:NSRunLoopCommonModes];
        }
        
        //1技师注册发送，2技师找回密码发送
        [XJHttpClass Sendmessagenumber:self.InputPhotoNumber.text messagetype:@"1" andFinishblock:^(id response, NSString *errStr) {
            
            if (errStr) {
                [XJToolClass timeChangeint:@"验证码发送失败,半小时内发送三次！"];
            }else{
                
                NSDictionary * datadict = response[@"data"];
                self.codeID =  [NSString stringWithFormat:@"%@",datadict[@"code_id"]];
                
            }
        }];
    }else{
        
        [XJToolClass tipAnimationWith:@"请输入正确的手机号码"];
        
    }
}

- (IBAction)SureChangeSecretMeth:(id)sender {
    
    if ([self.InputNewSecret.text isEqualToString:self.InputSureSecret.text]) {
        
        [XJHttpClass teachForgetSecretAccount:self.InputPhotoNumber.text new_pwd:self.InputSureSecret.text code_id:self.codeID code:self.VerificationCode.text andFinishblock:^(id response, NSString *errStr) {
            
            if (errStr) {
                [XJToolClass tipAnimationWith:errStr];
            }else{
                
                [XJToolClass tipAnimationWith:@"密码修改成功"];
                [self.navigationController popViewControllerAnimated:false];
            }
        }];
        
    }else{
        [XJToolClass tipAnimationWith:@"确认密码输入错误!"];
    }
}

// 倒计时
- (void)runLoop
{
    _runLoopTimes++;
    [self.VerificationReceiveBtn setTitle:[NSString stringWithFormat:@"重新发送(%@)", @(30-_runLoopTimes)] forState:UIControlStateNormal];
    
    if (_runLoopTimes == 30)
    {
        [unregisteredTimer invalidate];
        unregisteredTimer = nil;
        self.VerificationReceiveBtn.userInteractionEnabled = YES;
        _runLoopTimes = 0;
        [self.VerificationReceiveBtn setTitle:@"重新发送" forState:UIControlStateNormal];
    }
}

@end
